//
//  Global.swift
//  SwiftPhtotEdit
//
//  Created by aw031 on 13/10/2025.
//

import Foundation
import UIKit

public typealias StatusAndDataBlock = (_ success: Bool, _ result: Any?) -> Void



/// 主窗口
var KWindow: UIWindow? {
    return UIApplication.shared.windows.first{$0.isKeyWindow}
}


/// 共享应用程序
let KSharedApplication = UIApplication.shared

/// 应用程序代理
var KAppDelegate: AppDelegate? {
    return UIApplication.shared.delegate as? AppDelegate
}

// MARK: - 安全区域相关

/// 状态栏高度
var KStatusBarHeight: CGFloat {
    let topHeight = KWindow?.safeAreaInsets.top ?? 0
    return topHeight > 0 ? topHeight : 20.0
}

/// 导航栏高度
let KNavigationBarHeight: CGFloat = 44

/// 状态栏 + 导航栏高度
var KStatusAndNavigationHeight: CGFloat {
    return KStatusBarHeight + KNavigationBarHeight
}


/// 底部安全区域高度
var KTabBarBottom: CGFloat {
    return KWindow?.safeAreaInsets.bottom ?? 0
}

/// 标签栏高度
var KTabBarHeight: CGFloat {
    return KTabBarBottom + 49
}


/// 用户默认设置
let KUserDefaults = UserDefaults.standard

/// 通知中心
let KNotificationCenter = NotificationCenter.default

/// 用户通知中心
let KUserNotificationCenter = UNUserNotificationCenter.current()


/// 文件管理器
let KFileManager = FileManager.default

// MARK: - 屏幕尺寸相关


/// 屏幕高度
var KScreenWidth : CGFloat {
    UIScreen.main.bounds.width
}


/// 屏幕高度
var KScreenHeight : CGFloat {
    UIScreen.main.bounds.height
}


/// keyboard宽度
var KKeyboardWidth : CGFloat {
    UIScreen.main.bounds.width
}


/// keyboard高度
var KKeyboardHeight : CGFloat {
    UIScreen.main.bounds.width * 0.68
}


var KKeyboardSize : CGSize {
    CGSizeMake(KKeyboardWidth, KKeyboardHeight)
}
/// 屏幕高度
var KScreenBounds : CGRect {
    UIScreen.main.bounds
}


///// 根据屏幕宽度适配尺寸
///// - Parameter value: 设计稿尺寸
///// - Returns: 适配后的尺寸
//func KScreenWValue(_ value: CGFloat) -> CGFloat {
//    return (value / 375.0) * KScreenWidth
//}
//
///// 根据屏幕高度适配尺寸
///// - Parameter value: 设计稿尺寸
///// - Returns: 适配后的尺寸
//func KScreenHValue(_ value: CGFloat) -> CGFloat {
//    return (value / 812.0) * KScreenHeight
//}

func KScreenWValue(_ value: CGFloat) -> CGFloat {
    return (value / 375.0) * (KScreenWidth < KScreenHeight ? KScreenWidth : KScreenHeight)
}

/// 根据屏幕高度适配尺寸
/// - Parameter value: 设计稿尺寸
/// - Returns: 适配后的尺寸
func KScreenHValue(_ value: CGFloat) -> CGFloat {
    return (value / 812.0) * (KScreenWidth < KScreenHeight ? KScreenWidth : KScreenHeight)
}




// MARK: - 颜色相关

/// 从十六进制字符串创建颜色
/// - Parameter hexString: 十六进制颜色字符串
/// - Returns: UIColor 对象
func KColorHexString(_ hexString: String) -> UIColor {
    return UIColor.color(hexString:hexString)
}

// MARK: - 字符串验证

/// 检查字符串是否为空
/// - Parameter str: 要检查的字符串
/// - Returns: 是否为空
func KStringIsEmpty(_ str: String?) -> Bool {
    guard let str = str else { return true }
    return str.isEmpty ||
    str == "(null)" ||
    str == "nil" ||
    str == "<null>"
}

/// 检查对象是否为空
/// - Parameter object: 要检查的对象
/// - Returns: 是否为空
func KObjectIsEmpty(_ object: Any?) -> Bool {
    guard let object = object else { return true }
    
    if object is NSNull { return true }
    
    if let data = object as? Data, data.isEmpty { return true }
    if let array = object as? [Any], array.isEmpty { return true }
    if let dict = object as? [String: Any], dict.isEmpty { return true }
    
    return false
}

// MARK: - 日志

/// 调试日志
/// - Parameters:
///   - message: 日志消息
///   - file: 文件名
///   - function: 函数名
///   - line: 行号
func KLog(_ items: Any..., file: String = #file, function: String = #function, line: Int = #line) {
#if DEBUG
    let fileName = (file as NSString).lastPathComponent
    print("**********HGBErrorLog-start***********")
    print("{")
    print("文件名称: \(fileName);")
    print("方法: \(function);")
    print("行数: \(line);")
    print("提示: \(items)")
    print("}")
    print("**********HGBErrorLog-end***********")
#endif
}

func KLocalizedString(_ key: String) -> String {
    // 获取当前语言
    guard let languageCode = UserDefaults.standard.string(forKey: "TCAppLocalizedLanguage"),
          let path = Bundle.main.path(forResource: languageCode, ofType: "lproj"),
          let bundle = Bundle(path: path) else {
        return key
    }
    
    // 获取本地化字符串
    let localizedString = bundle.localizedString(forKey: key, value: nil, table: "Localizable")
    return localizedString.isEmpty ? key : localizedString
}

let KJumpHorWidth : CGFloat = 500
// MARK: - 版本信息

/// 获取当前版本号
var KVersion: String {
    return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "1.0.0"
}

/// 获取构建版本号
var KBuildVersion: String {
    return Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "1"
}

/// 获取完整版本信息
var KFullVersion: String {
    return "\(KVersion) (\(KBuildVersion))"
}


/// 检查是否为 iPad
var KIsIPad: Bool {
    return UIDevice.current.userInterfaceIdiom == .pad
}

/// 获取设备型号
var KDeviceModel: String {
    var systemInfo = utsname()
    uname(&systemInfo)
    let machineMirror = Mirror(reflecting: systemInfo.machine)
    let identifier = machineMirror.children.reduce("") { identifier, element in
        guard let value = element.value as? Int8, value != 0 else { return identifier }
        return identifier + String(UnicodeScalar(UInt8(value)))
    }
    return identifier
}

/// 获取系统版本
var KSystemVersion: String {
    return UIDevice.current.systemVersion
}

/// 检查系统版本是否大于等于指定版本
/// - Parameter version: 要比较的版本号
/// - Returns: 是否大于等于指定版本
func KIsSystemVersionGreaterThanOrEqualTo(_ version: String) -> Bool {
    return UIDevice.current.systemVersion.compare(version, options: .numeric) != .orderedAscending
}


//通知

// 在主程序中发送通知
func postKeyboardSkinChangeNotification() {
    let notificationName = KeyboardSkinDidChangeNotificationName as CFString
    CFNotificationCenterPostNotification(
        CFNotificationCenterGetDarwinNotifyCenter(),
        CFNotificationName(rawValue: notificationName),
        nil,
        nil,
        true
    )
}
//Kingfisher显示图片的时长
let KingfisherShowAnimationDuration = 0.2
//通知
//改变下载的贴纸
let KNotificantionChangeDownloadStickerNme = NSNotification.Name("ChangeDownloadStickerNme")

//发生更改主题
let KeyboardSkinDidChangeNotificationName = "KeyboardThemeDidChangeName"
//网络状态发生改变
let KNetworkStatusChangeNotificationName = NSNotification.Name("KNetworkStatusChangeNotificationName")
//vip发生改变
let KVipStatusChangeNotificationName = NSNotification.Name("KVipStatusChangeNotificationName")


//自定义键盘主题
//let CustomKeyboardThemeNotificationName = NSNotification.Name("CustomKeyboardThemeNotificationName")

//主程序与扩展程序的共享数据
let GroupAPPName = "group.com.cool.keyboardAndEmoji"
let KeyboardExtentsionAPPBundleID = "com.cool.keyboardAndEmoji.KeyboardExtension"



//保存主题键盘的版本号
let KkeyboardThemeLocalVersion = "KkeyboardThemeLocalVersion"
//保存贴纸的版本号
let KStickerLocalVersion = "KStickerLocalVersion"
//设置里面内容
let KAppSettingVersion = "KAppSettingVersion"
//配置里面的内容
let KAppConfigVersion = "KAppConfigVersion"




//程序跳转

let OpenURLSheme = "KeyboardAndEmojis"
//首页
let OpenUrlPushHomeKey = "Keyboard_Home"
//贴纸
let OpenUrlPushEmojiStickerKey = "Keyboard_Emoji_Sticker"
//设置页
let OpenUrlPushSettingKey = "Keyboard_Setting"

//APPID
let KAppID = "6751635422"
let KAppFlayerID = "nhu2y2s8yeV5rdyD8X9cAk"

let KAppLocalizedLanguageName = "AppLocalizedLanguage"




//隐私协议
let KPrivacyPolicyURL = "https://ethanus.com/keyboard/privacy.html"
//用户声明
let KUserOfTermsURL = "https://ethanus.com/keyboard/terms.html"



//API 接口
let KBaseURL = "https://k.ethanus.com"


// 提示语
//相机授权提示语
let CameraAuthorizationFailedTip = "Camera access failed. Please allow access in settings to use the camera function."
//相册授权提示语
let AlbumsAuthorizationFailedTip = "Unable to access the photo album. Please allow access in settings to use the photo album feature."

//设置不支持
let NotSupportCameraTip = "This device does not support taking photos yet."
